package impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.ParticipantDao;
import util.C3P0Util;

public class ParticipantDaoImpl implements ParticipantDao {

	@Override
	public List<Integer> queryUserIdByItemId(int itemId) throws SQLException {
		Connection connection = null;
		PreparedStatement ps = null;
		List<Integer > list = new ArrayList<Integer>();
		try {
			connection = C3P0Util.getConnection();
			// 不插入id 因为id为自增的
			String sql = "select personnel_id from t_participant where item_id=?";
			ps = (PreparedStatement) connection.prepareStatement(sql);		
			ps.setInt(1, itemId);
			ResultSet rs =  ps.executeQuery();
			while(rs.next()){
				list.add(rs.getInt("personnel_id"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			C3P0Util.close(connection);
		}
		return list;
	}

	@Override
	public List<Integer> queryItemIdByUserId(int userId) throws SQLException {
		Connection connection = null;
		PreparedStatement ps = null;
		List<Integer > list = new ArrayList<Integer>();
		try {
			connection = C3P0Util.getConnection();
			// 不插入id 因为id为自增的
			String sql = "select item_id from t_participant where personnel_id=?";
			ps = (PreparedStatement) connection.prepareStatement(sql);		
			ps.setInt(1, userId);
			ResultSet rs =  ps.executeQuery();
			while(rs.next()){
				list.add(rs.getInt("item_id"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			C3P0Util.close(connection);
		}
		return list;
	}

}
